﻿Imports DTO
Imports System.Data.SqlClient

Public Class CLoaiDocGiaDAO
    ' Load danh sách loại độc giả:
    Public Function LoadDanhSachLoaiDocGia() As List(Of CLoaiDocGiaDTO)
        Using cn As SqlConnection = CDataProvider.Connect()
            Dim cm As SqlCommand = New SqlCommand("uspLayDanhSachLoaiDocGia", cn)
            cm.CommandType = CommandType.StoredProcedure
            Dim listloaidocgia As New List(Of CLoaiDocGiaDTO)()
            Using reader As SqlDataReader = cm.ExecuteReader(CommandBehavior.CloseConnection)
                While reader.Read()
                    listloaidocgia.Add(New CLoaiDocGiaDTO(reader))
                End While
            End Using
            Return listloaidocgia
        End Using
    End Function


    Public Function ThemLoaiDocGia(ByVal tenloaidocgia As String) As Integer
        Using cn As SqlConnection = CDataProvider.Connect()
            Dim cm As New SqlCommand("usp_ThemLoaiDocGia", cn)
            cm.CommandType = CommandType.StoredProcedure
            Dim para As New SqlParameter("@tenloaidocgia", tenloaidocgia)
            cm.Parameters.Add(para)
            Dim parakq As New SqlParameter("@kt", 0)
            parakq.Direction = ParameterDirection.Output
            cm.Parameters.Add(parakq)
            cm.ExecuteNonQuery()
            Dim kq As Integer = Integer.Parse(cm.Parameters("@kt").Value.ToString())
            cn.Close()
            Return kq
        End Using
    End Function


    Public Function CapNhatLoaiDocGia(ByVal loaidocgia As CLoaiDocGiaDTO) As Integer
        Using cn As SqlConnection = CDataProvider.Connect()
            Dim cm As New SqlCommand("usp_CapNhatLoaiDocGia", cn)
            cm.CommandType = CommandType.StoredProcedure
            Dim para As New SqlParameter("@maloaidocgia", loaidocgia.Ma)
            cm.Parameters.Add(para)
            para = New SqlParameter("@tenloaidocgia", loaidocgia.Ten)
            cm.Parameters.Add(para)
            Dim parakt As New SqlParameter("@kt", 0)
            parakt.Direction = ParameterDirection.Output
            cm.Parameters.Add(parakt)

            cm.ExecuteNonQuery()
            Dim kq As Integer = Integer.Parse(cm.Parameters("@kt").Value.ToString())
            cn.Close()
            Return kq
        End Using
    End Function

End Class
